import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

class ListViewRefreshIndicatorPage extends StatefulWidget {
  final Map<String, dynamic> arguments;
  ListViewRefreshIndicatorPage({Key key, this.arguments}) : super(key: key);

  @override
  _ListViewRefreshIndicatorPageState createState() => _ListViewRefreshIndicatorPageState();
}

class _ListViewRefreshIndicatorPageState extends State<ListViewRefreshIndicatorPage> {
  ScrollController scrollController = ScrollController();
  List<JokeModel> jokeModels = List();
  int page = 1;

  @override
  void initState() {
    super.initState();
    scrollController.addListener(() {
      print(scrollController.position.pixels);
      if (scrollController.position.pixels == scrollController.position.maxScrollExtent) {
        pullUpRefresh();
      }
    });

    // 初始数据加载
    pullDownRefresh();
  }

  @override
  void dispose() {
    super.dispose();

    scrollController.dispose();
  }

  /// 下拉刷新
  Future<Null> pullDownRefresh() async {
    List<JokeModel> jokeModels = await fetchModel(page: 1);
    this.page = 1;
    this.jokeModels.clear();
    this.jokeModels.addAll(jokeModels);

    setState(() {});
  }

  /// 上拉刷新
  Future<Null> pullUpRefresh() async {
    final loadPage = page + 1;
    List<JokeModel> jokeModels = await fetchModel(page: loadPage);
    this.page = loadPage;
    this.jokeModels.addAll(jokeModels);

    setState(() {});
  }

  /// 网络获取数据
  Future<List<JokeModel>> fetchModel({@required int page}) async {
    Map<String, dynamic> params = {
      'page': page,
      'count': 10,
      'type': 'video',
    };
    final response = await Dio().get('https://api.apiopen.top/getJoke', queryParameters: params);

    List result = response.data['result'];
    final jokeModels = result.map((map) => JokeModel.fromJson(map)).toList();

    return jokeModels;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ListViewRefreshIndicatorPage'),
      ),
      body: RefreshIndicator(
        onRefresh: pullDownRefresh,
        child: ListView(
          controller: this.scrollController,
          children: buildList(),
        ),
      ),
    );
  }

  List<Widget> buildList() => this.jokeModels.map((m) => this.cell(m)).toList();

  Widget cell(JokeModel m) => ListTile(
        title: Text(m.name),
        subtitle: Text(m.text),
      );
}

class JokeModel {
  final String text;
  final String thumbnail;
  final String name;
  JokeModel.fromJson(Map<String, dynamic> json)
      : this.text = json['text'],
        this.thumbnail = json['thumbnail'],
        this.name = json['name'];
}

final _json = {
  "sid": "31176707",
  "text": "那你嫁给她哥。",
  "type": "video",
  "thumbnail": "http://wimg.spriteapp.cn/picture/2020/0425/5ea407e8d6f5d_wpd.jpg",
  "video": "http://uvideo.spriteapp.cn/video/2020/0425/5ea407e8d6f5d_wpd.mp4",
  "images": null,
  "up": "116",
  "down": "18",
  "forward": "0",
  "comment": "12",
  "uid": "23132731",
  "name": "熙熙",
  "header": "http://wimg.spriteapp.cn/profile/large/2019/07/04/5d1d9691ae965_mini.jpg",
  "top_comments_content": "不得姐我有点坚守不下去了",
  "top_comments_voiceuri": "",
  "top_comments_uid": "8881182",
  "top_comments_name": "御弟哥哥",
  "top_comments_header": "http://wimg.spriteapp.cn/profile/large/2018/09/23/5ba6d95f3996d_mini.jpg",
  "passtime": "2020-04-28 19:49:02"
};
